/**
 * 服务接口
 * @author  Jello
 * @create  2018/10/16 10:45
 * @version 1.0
 * @company 广东蓝聘科技网络有限公司
 **/

var serverTimeOut = 15000; //请求接口超时时间

/**
 * 获取验证码接口,返回token
 * @param {Object} param
 * @param {Object} callback
 */
function gainCode(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/login/send_code?phone=" + param.phone + "&i=" + param.type,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: serverTimeOut,
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			spinner.spin();
			var resultObj = eval(data);
			//			console.log(JSON.stringify(resultObj));
			if(resultObj.code == 0) {
				var token = resultObj.data; //获取验证码成功后返回的token
				mui.toast("验证码已发送,请注意查收");
				callback(token);
			} else {
				mui.toast(data.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			spinner.spin();
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 校验验证码接口
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function mateCode(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/login/mate_code?phone=" + param.phone + "&i=" + param.type + "&code=" + param.code + "&token=" + param.token,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			var mateObj = eval(data);
			//			console.log("忘记密码-获取验证码接口：" + JSON.stringify(mateObj));
			if(mateObj.code == 0) {
				callback();
			} else {
				spinner.spin();
				mui.toast(mateObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			spinner.spin();
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 获取个人资料
 * @param {Object} param 参数
 * @param {Object} callback 回调 
 */
function getUserMaterial(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/userMaterial/select?sgin=" + param.userid_sign,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		success: function(data) {
			var materialObj = eval(data);
			//			console.log("获取个人资料接口：" + JSON.stringify(materialObj));
			if(materialObj.code == 0 && materialObj.data != null) {
				callback(materialObj.data);
			} else {
				mui.toast(materialObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询是否绑定银行卡
 * @param {Object} param
 * @param {Object} callback
 */
function queryIsBindCard(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/userbank/select_bindbankcard?sgin=" + param.userid_sign,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			spinner.spin();
			var bankObj = eval(data);
			if(bankObj.code == 0) {
				//				console.log("查询工资卡信息接口：" + JSON.stringify(bankObj));
				callback(bankObj);
			} else {
				$.myToast(bankObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			spinner.spin();
			if(status == 'timeout') {
				$.myToast("请求超时");
			} else {
				$.myToast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 校验提现密码，完成身份验证
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function checkWithdrawPwd(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/userbank/update_drawpwd?sgin=" + param.userid_sign + "&oldPwd=" + param.oldPwd + "&type=" + param.type + "&withdrawPwd=" + param.withdrawPwd,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		success: function(data) {
			//			console.log("校验提现密码接口：" + JSON.stringify(data));
			var resultObj = eval(data);
			callback(resultObj);
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			if(status == 'timeout') {
				$.myToast("请求超时");
			} else {
				$.myToast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询个人账单 
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function queryPersonBill(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/billrecord/query_billrecord?sgin=" + param.userid_sgin + "&page=" + param.page,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			spinner.spin();
			var billObj = eval(data);
			//			console.log("查询个人账单接口：" + JSON.stringify(billObj));
			if(billObj.code == 0) {
				callback(billObj.data);
			} else {
				$.myToast(billObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			spinner.spin();
			if(status == 'timeout') {
				$.myToast("请求超时");
			} else {
				$.myToast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询招聘列表
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function queryWorkList(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_worklist?identity=" + param.identity + "&page=" + param.page + "&mechanismTypeId=" + param.mechanismType + "&workType=" + param.workType + "&orderType=" + param.orderType + "&type=" + param.type + "&mechanismAddress=" + param.city + "&mechanismName=" + param.mechanismName + "&sgin=" + param.userid_sign,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var workList = eval(data);
			//			console.log("查询招聘列表接口：" + JSON.stringify(workList));
			if(workList.code == 0) {
				callback(workList);
			} else {
				mui.toast(workList.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询所有行业及工种
 * @param {Object} callback
 */
function queryMechanismList(callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_mechanismlist",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: serverTimeOut,
		dataType: "json",
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			//			console.log("查询所有行业接口：" + JSON.stringify(resultObj));
			if(resultObj.code == 0) {
				callback(resultObj);
			} else {
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 入职报名
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function entryApply(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/work/entryApply?sgin=" + userid_sign + "&type=" + param.type,
		data: "{\"reTime\":\"" + param.reTime + "\",\"reMoney\":\"" + param.reMoney + "\",\"identity\":\"" + param.identy + "\",\"interviewTime\":\"" + param.interviewTime + "\",\"mechanismId\":\"" + param.mechanismId + "\",\"mechanismName\":\"" + param.mechanismName + "\",\"userName\":\"" + param.userName + "\",\"workId\":\"" + param.workId + "\",\"recruitAddress\":\"" + param.recruitAddress + "\"}",
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			$(".succ_divbox").css("display", "none");
			$(".succ_box").css("display", "none");
			var resultObj = eval(data);
			//			console.log("入职报名接口：" + JSON.stringify(resultObj));
			if(resultObj.code == 0 && resultObj.data != null) {
				callback(resultObj);
			} else {
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			$(".succ_divbox").css("display", "none");
			$(".succ_box").css("display", "none");
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 取消报名
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function cancelApply(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/work/cancleApply?sgin=" + param.userid_sign + "&workId=" + param.workId,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			if(resultObj.code == 0 && resultObj.data != null) {
				callback(resultObj);
			} else {
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {
			closeSpin(300);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询招聘详情
 * @param {Object} param 参数
 * @param {Object} callback 回调
 */
function queryWorkDetail(param, callback) {
	//查询详情
	$.ajax({
		type: "get",
		url: serverUrl + "/work/query_workdetail?workId=" + param.workId,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			closeSpin(300);
			var resultObj = eval(data);
			//			console.log("查询招聘详情接口：" + JSON.stringify(resultObj));
			if(resultObj.code == 0 && resultObj.data != null) {
				callback(resultObj);
			} else {
				closeSpin(300);
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询借支记录
 * @param {Object} param
 * @param {Object} callback
 */
function queryLendRecord(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/lendmoney/query_checkrecord?sgin=" + param.userid_sign,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		timeout: serverTimeOut,
		dataType: "json",
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			//			console.log("查询是否有借支记录接口：" + JSON.stringify(resultObj));
			if(resultObj.code == 0) {
				callback(resultObj);
			} else {
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 微信绑定手机号
 * @param {Object} param
 * @param {Object} callback
 */
function bindCustomerPhone(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/wx_user/set_phone?sgin=" + param.userid_sign + "&phone=" + param.phone + "&userName=" + param.userName + "&userUrl=" + param.userUrl,
		async: false,
		cache: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		crossDomain: true,
		timeout: serverTimeOut,
		success: function(data) {
			var resultObj = eval(data);
			//			console.log("微信绑定客户手机号接口：" + JSON.stringify(resultObj));
			if(resultObj.code == 0 && resultObj.data == 1) {
				callback();
			} else {
				mui.toast(resultObj.msg);
				return;
			}
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(300);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 添加一笔借支
 * @param {Object} param
 * @param {Object} callback
 */
function addLendMoney(param, callback) {
	$.ajax({
		type: "post",
		url: serverUrl + "/lendmoney/add_lendmoney_api?sgin=" + param.openid + "&lendMoney=" + param.lendMoney + "&mechanismId=" + param.mechanismId + "&teacherTel=" + param.teacherTel,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: serverTimeOut,
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			var resultObj = eval(data);
			closeSpin(100);
			callback(resultObj);
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(100);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}

/**
 * 查询借支公司列表
 * @param {Object} param
 * @param {Object} callback
 */
function queryLendCompanyList(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/lendmoney/query_mechanism?sgin=" + param.openid + "&page=" + param.page + "&size=" + param.size,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: serverTimeOut,
		crossDomain: true,
		success: function(data) {
			var resultObj = eval(data);
			callback(resultObj);
		}
	});
}

/**
 * 查询我的工资列表
 * @param {Object} param
 * @param {Object} callback
 */
function querySalaryList(param, callback) {
	$.ajax({
		type: "get",
		url: serverUrl + "/billrecord/query_salarylist?sgin=" + param.openid + "&month=" + param.month,
		cache: false,
		async: false,
		contentType: "application/json; charset=utf-8",
		dataType: "json",
		timeout: serverTimeOut,
		crossDomain: true,
		beforeSend: function() {
			$("#myspin").text("");
			var target = $("#myspin").get(0);
			spinner.spin(target);
		},
		success: function(data) {
			var resultObj = eval(data);
			closeSpin(100);
			callback(resultObj);
		},
		error: function(XMLHttpRequest, status, errorThrown) {
			closeSpin(100);
			if(status == 'timeout') {
				mui.toast("请求超时");
			} else {
				mui.toast("请求网络服务数据失败");
			}
			return;
		}
	});
}